<?php

namespace Boosen\Company\app\Console\Commands;

use Illuminate\Console\Command;

use DB;
use Boosen\Company\app\Models\CNBaseUrl;

use Goutte\Client;
use GuzzleHttp\Client as GuzzleClient;
use Symfony\Component\DomCrawler\Link;

class CNBaseUrlSpider extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cn:base_url';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $urls = config('boosen.company.cn_base_urls');
        foreach ($urls as $url) {
            $this->processCityLinks($url);
        }
    }

    /**
     * [getProvinceUrl description]
     * @param  [type] $url [description]
     * @return array \object
     */
    private function getProvinceUrl($url)
    {
        try{
            $links = array();
            $goutteClient = new Client();
            $guzzleClient = new GuzzleClient(array(
                'timeout' => 60,
            ));
            $goutteClient->setClient($guzzleClient);

            $crawler = $goutteClient->request('GET', $url);
            $provinceBox = $crawler->filterXPath('//div[@class="screen_box"]/div')->first();
            return $provinceBox->filterXPath('//ul/li/a')->links();

        } catch (\Exception $e) {
            echo $e->getMessage();
            exit;
        }
    }

    private function processCityLinks($url)
    {
        $provinceLinks = $this->getProvinceUrl($url);
        try {
            $goutteClient = new Client();
            $guzzleClient = new GuzzleClient(array(
                'timeout' => 60,
            ));
            $goutteClient->setClient($guzzleClient);

            $total = 0;
            foreach ($provinceLinks as $link) {
                if (strposa($link->getUri(), array('beijing', 'shanghai', 'chongqing', 'tianjin'), 7)) {
                    $clinks = [$link];
                } else {
                    $crawler = $goutteClient->click($link);
                    $keywords = mb_substr($crawler->filterXPath('//div[@class="location_an"]/h1')->text(), -3);

                    $cityBox = $crawler->filterXPath('//div[@class="screen_box"]/div')->eq(1);
                    $clinks = $cityBox->filterXPath('//ul/li/a')->links();
                    // print_r($aTags);exit;
                }
                $subTotal = 0;
                foreach ($clinks as $clink) {
                    $total++;
                    $subTotal++;
                    $node = $clink->getNode();
                    $res = CNBaseUrl::updateOrCreate(
                        ['keywords' => $keywords, 'city' => trim($node->nodeValue)],
                        ['url' => $clink->getUri(), 'scode' => $node->getAttribute('data-scode')]
                    );
                    if ($res) {
                        $this->info("Create or update a new item: " . $node->nodeValue . $keywords . ' ');
                    } else {
                        $this->error("Failed to Create or update a new item: " . $node->nodeValue . $keywords . ' ');
                    }
                    
                }

                $this->info($subTotal . ' items insert Ok for province ' . $link->getNode()->nodeValue . $keywords);
                sleep(2);

            }
            // $this->info($total . ' items insert Ok for ' . $keywords);
        } catch(\Exception $e) {
            echo $e->getMessage();
            exit;
        }
    }
}
